package zirao.cn.http;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import zirao.cn.http.callback.FileCallback;
import zirao.cn.http.callback.StringCallback;
import zirao.cn.http.model.Progress;
import zirao.cn.http.model.Response;
import zirao.cn.http.request.base.Request;

import java.io.File;

/**
 * Created by ${lixiaonan} on 2017/12/19.
 */
public class MyHttpClient {

    private Context                  mContext;
    private static MyHttpClient     myHttpClient;

    public static MyHttpClient getInstance(){
        if (null == myHttpClient){
            myHttpClient = new MyHttpClient();
        }
        return myHttpClient;
    }

    public void init(Application app){
        OkGo.getInstance().init(app);
        mContext = app;
    }

    public void cancelTag(Context context){
        OkGo.getInstance().cancelTag(context);
    }

    //------------------get方法-----------------------
    public void get(Context context, String url, MyHttpParams params, final MyHttpCallBack callback){
        OkGo.<String>get(url)
            .tag(context)
            .params(params)
            .execute(new StringCallback() {
                @Override
                public void onStart(Request<String, ? extends Request> request) {
                    super.onStart(request);
                    try {
                        callback.onStart();
                    }catch (Exception e){
                        Log.e("HttpCore", e.getMessage());
                    }
                }

                @Override
                public void onSuccess(Response<String> response) {
                    try {
                        callback.onSuccess(response.body());
                    }catch (Exception e){
                        callback.onFailure(e.getMessage()  + "\n" + response.body());
                        Log.e("HttpCore", e.getMessage() + "\n" + response.body());
                    }
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    try {
                        callback.onFailure("ErrorCode:" + response.code() + " ErrorTip:" + response.message());
                    }catch (Exception e){
                        Log.e("HttpCore", e.getMessage());
                    }
                }
            });
    }

    //------------------post方法-----------------------
    public void post(Context context, String url, MyHttpParams params, final MyHttpCallBack callback){
        OkGo.<String>post(url)
        .tag(this)
        .params(params)
        .execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                try {
                    callback.onStart();
                }catch (Exception e){
                    Log.e("HttpCore", e.getMessage());
                }
            }

            @Override
            public void onSuccess(Response<String> response) {
                try {
                    callback.onSuccess(response.body());
                }catch (Exception e){
                    callback.onFailure(e.getMessage()  + "\n" + response.body());
                    Log.e("HttpCore", e.getMessage() + "\n" + response.body());
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                try {
                    callback.onFailure("ErrorCode:" + response.code() + " ErrorTip:" + response.message());
                }catch (Exception e){
                    Log.e("HttpCore", e.getMessage());
                }
            }
        });
    }

    //------------------downloadFile方法-----------------------
    public void download(Context context, String url, String driPath, String fileName, final MyHttpCallBack callback){
        OkGo.<File>get(url)
            .tag(this)
            .execute(new FileCallback(driPath, fileName) {

                @Override
                public void onStart(Request<File, ? extends Request> request) {
                    super.onStart(request);
                    try {
                        callback.onStart();
                    }catch (Exception e){
                        Log.e("HttpCore", e.getMessage());
                    }
                }

                @Override
                public void onSuccess(Response<File> response) {
                    try {
                        callback.onDownloadSuccess(response.body());
                    }catch (Exception e){
                        callback.onFailure(e.getMessage()  + "\n" + response.body());
                        Log.e("HttpCore", e.getMessage()  + "\n" + response.body());
                    }
                }

                @Override
                public void onError(Response<File> response) {
                    super.onError(response);
                    try {
                        callback.onFailure("ErrorCode:" + response.code() + " ErrorTip:" + response.message());
                    }catch (Exception e){
                        Log.e("HttpCore", e.getMessage());
                    }
                }

                @Override
                public void downloadProgress(Progress progress) {
                    super.downloadProgress(progress);
                    try {
                        callback.onProcess(progress.totalSize, progress.currentSize, progress.fraction, progress.speed);
                    }catch (Exception e){
                        Log.e("HttpCore", e.getMessage());
                    }
                }
            });
    }
}